#define BLINKER_WIFI

#include <Blinker.h>

char auth[] = "";//skey
char ssid[] = "";//WiFi名
char pswd[] = "";//密码

// 新建组件对象
BlinkerButton Button1("btn-abc");
BlinkerButton gensui("btn-hd");
BlinkerButton ziyou("btn-zy");
BlinkerButton shoudong("btn-sd");
BlinkerNumber Number1("num-abc");
BlinkerButton r1("btn-r1");
BlinkerButton r2("btn-r2");
BlinkerButton r3("btn-r3");
BlinkerButton l1("btn-l1");
BlinkerButton l2("btn-l2");
BlinkerButton l3("btn-l3");

int counter = 0;

// 按下按键即会执行该函数
void button1_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}

void r1_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(16,LOW); //右
    digitalWrite(4,LOW);
    digitalWrite(2,LOW);
}

void r2_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(16,LOW); //右
    digitalWrite(4,HIGH);
    digitalWrite(2,LOW);
}

void r3_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(16,LOW); //右
    digitalWrite(4,LOW);
    digitalWrite(2,HIGH);
}

void l1_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(16,HIGH); //左
    digitalWrite(4,LOW);
    digitalWrite(2,LOW);
}

void l2_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(16,HIGH); //左
    digitalWrite(4,HIGH);
    digitalWrite(2,LOW);
}

void l3_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(16,HIGH); //左
    digitalWrite(4,LOW);
    digitalWrite(2,HIGH);
}

void gensui_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(5,LOW);  
    digitalWrite(0,LOW);
}

void ziyou_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(5,LOW);  
    digitalWrite(0,HIGH);
}

void shoudong_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(5,HIGH); 
    digitalWrite(0,LOW);
}

// 如果未绑定的组件被触发，则会执行其中内容
void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);
    counter++;
    Number1.print(counter);
}

void setup()
{
    // 初始化串口
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    BLINKER_DEBUG.debugAll();
    // 初始化IO
    pinMode(0,OUTPUT);//3
   // pinMode(4,OUTPUT);//2
    pinMode(5,OUTPUT);//1
    pinMode(16,OUTPUT);//0
    pinMode(4,OUTPUT);//8
    pinMode(2,OUTPUT);//4
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(0, LOW);
    digitalWrite(16, LOW);
    digitalWrite(4, LOW);
    digitalWrite(2, LOW);
    // 初始化blinker
    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);

    Button1.attach(button1_callback);
    gensui.attach(gensui_callback);
    ziyou.attach(ziyou_callback);
    shoudong.attach(shoudong_callback);
    r1.attach(r1_callback);
    r2.attach(r2_callback);
    r3.attach(r3_callback);
    l1.attach(l1_callback);
    l2.attach(l2_callback);
    l3.attach(l3_callback);
}

void loop() {
    Blinker.run();
}
